package de.idgard.android.addon.ui.services.impl;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import de.idgard.android.addon.services.ContextAware;
import de.idgard.android.addon.ui.services.LayoutAdjustmentService;

/**
 * @author pratik popat
 * 
 */
class LayoutAdjustmentServiceImpl implements LayoutAdjustmentService,ContextAware{
	
	private Context context;
	
	@Override
	public void adjustDialogLayout(Dialog dialog, int height, int width, int gravity, int flags) {
		
		/* proper adjustment as per screen height width */
		if(width >= getScreenHeight())
		width = getScreenHeight() *((width*100)/getScreenWidth())/100;	
		
		if(height >= getScreenWidth())
		height = getScreenWidth() *((height*100)/getScreenHeight())/100;

		
		LayoutParams lp=dialog.getWindow().getAttributes();    
		
		if(width != -1)
		lp.width = width;
		else
		lp.width = LayoutParams.FILL_PARENT;
		
		if(height != -1)
		lp.height = height;
		else
		lp.height = LayoutParams.WRAP_CONTENT;
		
		if(gravity != -1)
		lp.gravity = gravity;
		else
		lp.gravity = Gravity.CENTER;
		
	    if(flags != -1)
		lp.flags = flags;
	    
        /* @info
         * Layout flags for touch dialog behind screen.
	     * [ LayoutParams.FLAG_LAYOUT_NO_LIMITS | LayoutParams.FLAG_NOT_TOUCH_MODAL ]
	     */ 
	    
        dialog.getWindow().setAttributes(lp);
	}

	@Override
	public int getScreenWidth() {
		WindowManager windowMgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		return windowMgr.getDefaultDisplay().getWidth();
	}

	@Override
	public int getScreenHeight() {
		WindowManager windowMgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		return windowMgr.getDefaultDisplay().getHeight();
	}

	@Override
	public void setContext(Context ctx) {
		context = ctx;
	}

}
